package http

import (
	"5title_gin/pkg/utils/validator"
	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int    `json:"code"`
	Data any    `json:"data"`
	Msg  string `json:"message"`
}

type PageResult struct {
	List     any   `json:"list"`
	Total    int64 `json:"total"`
	Page     int   `json:"page"`
	PageSize int   `json:"pageSize"`
}

func Ok(data any, msg string, c *gin.Context) {
	c.JSON(200, Response{
		Code: 200,
		Data: data,
		Msg:  msg,
	})
}

func OkWithMsg(msg string, c *gin.Context) {
	Ok(gin.H{}, msg, c)
}
func OkWithData(data any, c *gin.Context) {
	Ok(data, "成功", c)
}
func OkWithList(list any, count int64, c *gin.Context) {
	Ok(map[string]any{"list": list, "count": count}, "成功", c)

}
func Fail(code int, msg string, c *gin.Context) {
	c.JSON(200, Response{
		Code: code,
		Data: gin.H{},
		Msg:  msg,
	})
}

func FailWithMsg(msg string, c *gin.Context) {
	Fail(401, msg, c)
}

func FailWithError(err error, c *gin.Context) {
	msg := validator.ValidateErr(err)
	Fail(500, msg, c)
}
func Error(msg string, err error, c *gin.Context) {
	c.JSON(200, Response{
		Code: 500,
		Data: validator.ValidateErr(err),
		Msg:  msg,
	})
}
